package com.algorithm.practice.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.algorithm.practice.R;

public class LinkedListFragment extends Fragment {

    private static final String TAG = "LinkedListFragment";

    static {
        System.loadLibrary("test-linkedlist");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_linked_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RadioGroup radioGroup = view.findViewById(R.id.frag_linked_list_rg);
        tip = view.findViewById(R.id.frag_linked_list_tip);
        editText = view.findViewById(R.id.layout_input_edit);
        Button button = view.findViewById(R.id.layout_input_btn);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.frag_linked_list_rb:
                        type = 0;
                        break;
                    case R.id.frag_linked_list_rb2:
                        type = 1;
                        break;
                    case R.id.frag_linked_list_rb3:
                        type = 2;
                        break;
                }
            }
        });
        tip.setText("测试是否是回文串");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getEditableText().toString().trim();
                try {
                    final String tipText;
                    if (isPalindromeString(s, type)) {
                        tipText = "是回文串";
                    } else {
                        tipText = "不是回文串";
                    }
                    tip.setText(tipText);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
                editText.setText("");
            }
        });
    }

    private byte type = 0;

    private TextView tip;

    private EditText editText;

    final native boolean isPalindromeString(String src, byte type);
}
